package com.bjx.system.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjx.common.core.constant.FrameConst;
import com.bjx.common.core.exception.BJXException;
import com.bjx.common.security.domain.LoginUserVO;
import com.bjx.common.security.util.LoginUtil;
import com.bjx.system.domain.entity.MenuEntity;
import com.bjx.system.domain.vo.MenuVO;
import com.bjx.system.mapper.MenuMapper;
import com.bjx.system.service.IMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @desc: 菜单服务
 * @author: liushuai
 * @date: 2022/7/6 13:52
 */
@Service
@Slf4j
public class MenuServiceImpl extends ServiceImpl<MenuMapper, MenuEntity> implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Tree<String>> listMenuAuthTree() {
        List<MenuEntity> menuEntities = menuMapper.selectList(new LambdaQueryWrapper<>());
        List<Tree<String>> treeNodes = TreeUtil.build(menuEntities, FrameConst.ROOT_ID_STR,
                (menu, tree) -> {
                    tree.setId(String.valueOf(menu.getMenuId()));
                    tree.setParentId(String.valueOf(menu.getPid()));
                    tree.setWeight(menu.getOrderNum());
                    tree.setName(menu.getMenuName());
                    tree.putExtra("menuId", String.valueOf(menu.getMenuId()));
                    tree.putExtra("menuName", menu.getMenuName());
                    tree.putExtra("type", menu.getType());
                    tree.putExtra("authFlag",menu.getAuthFlag());
                    setMenuTreeNode(tree,menu);
                });
        return treeNodes;
    }

    @Override
    public List<Long> listPidAll() {
        LambdaQueryWrapper<MenuEntity> wrapper = new LambdaQueryWrapper();
        wrapper.select(MenuEntity::getPid);
        List<Long> pids = menuMapper.selectList(wrapper).stream().map(MenuEntity::getPid).collect(Collectors.toList());
        return pids;
    }



    @Override
    public void insertMenu(MenuVO menuVO) {
        //校验菜单名称是否重复
        if(StrUtil.isNotEmpty(menuVO.getMenuName())){
            LambdaQueryWrapper<MenuEntity> wrapper = new LambdaQueryWrapper();
            wrapper.eq(MenuEntity::getMenuName,menuVO.getMenuName());
            Long count = menuMapper.selectCount(wrapper);
            if(count > 0){
                throw new BJXException("菜单名称已存在");
            }
        }
        MenuEntity menu = Convert.convert(MenuEntity.class, menuVO);
        this.save(menu);
    }

    @Override
    public List<Tree<String>> listMenuTreeByLoginUser() {
        LoginUserVO loginUserVO = LoginUtil.loginInfo();
//        LambdaQueryWrapper<MenuEntity> wrapper = new LambdaQueryWrapper();
//        wrapper.eq(MenuEntity::getEnabled, FrameConst.YES);
//        wrapper.in(MenuEntity::getType, MenuConst.MENU,MenuConst.DIRECTORY);
//        List<MenuEntity> menus = menuMapper.selectList(wrapper);
//        if(!"1".equals(loginUserVO.getUserId())){
//
//        }
        List<MenuEntity>  menus = menuMapper.listUserMenuByUserId(Long.valueOf(loginUserVO.getUserId()));
        List<Tree<String>> treeNodes = TreeUtil.build(menus, "0",
                (menu, tree) -> {
                    tree.setId(String.valueOf(menu.getMenuId()));
                    tree.setParentId(String.valueOf(menu.getPid()));
                    tree.setWeight(menu.getOrderNum());
                    tree.setName(menu.getMenuName());
                    //设置扩展属性
                    setMenuTreeNode(tree,menu);
                });
        return treeNodes;
    }



    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateMenu(MenuVO menuVO) {
        MenuEntity entity = Convert.convert(MenuEntity.class, menuVO);
        menuMapper.updateById(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteMenuByMenuId(Long menuId) {
        menuMapper.deleteById(menuId);
    }

    @Override
    public MenuVO selectMenuByMenuId(Long menuId) {
        MenuEntity entity = menuMapper.selectById(menuId);
        MenuVO vo = Convert.convert(MenuVO.class,entity);
        return vo;
    }



    private void setMenuTreeNode(Tree<String> tree,MenuEntity menu){
        tree.putExtra("title", menu.getMenuTitle());
        tree.putExtra("titleMenu", menu.getMenuTitle());
        tree.putExtra("hidden", menu.getHidden());
        tree.putExtra("affix", menu.getAffix());
        tree.putExtra("requestPath", menu.getRequestPath());
        tree.putExtra("filePath",menu.getFilePath());
        tree.putExtra("params",menu.getParams());
        tree.putExtra("icon",menu.getIcon());
        tree.putExtra("menuType",menu.getMenuType());
    }
}
